<template>
  <div class="announcements-container">
    <el-card class="announcements-card">
      <template #header>
        <div class="card-header">
          <h3>系统公告</h3>
        </div>
      </template>

      <!-- 公告列表 -->
      <div v-if="announcements.length" class="announcements-list">
        <el-timeline>
          <el-timeline-item
            v-for="announcement in announcements"
            :key="announcement.id"
            :timestamp="announcement.createTime"
            placement="top"
          >
            <el-card class="announcement-item">
              <template #header>
                <div class="announcement-header">
                  <h4>{{ announcement.title }}</h4>
                  <el-tag 
                    :type="announcement.type === 'important' ? 'danger' : 'info'"
                    size="small"
                  >
                    {{ announcement.type === 'important' ? '重要' : '普通' }}
                  </el-tag>
                </div>
              </template>
              <div class="announcement-content">
                {{ announcement.content }}
              </div>
            </el-card>
          </el-timeline-item>
        </el-timeline>
      </div>

      <!-- 空状态 -->
      <el-empty v-else description="暂无公告" />

      <!-- 分页 -->
      <div class="pagination" v-if="announcements.length">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[5, 10, 20, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getAnnouncements } from '../api'

const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const announcements = ref([])

const loadAnnouncements = async () => {
  try {
    const params = {
      page: currentPage.value,
      pageSize: pageSize.value
    }
    const { data, total: totalCount } = await getAnnouncements(params)
    announcements.value = data
    total.value = totalCount
  } catch (error) {
    ElMessage.error('获取公告失败')
  }
}

const handleSizeChange = (val) => {
  pageSize.value = val
  loadAnnouncements()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  loadAnnouncements()
}

onMounted(() => {
  loadAnnouncements()
})
</script>

<style scoped>
.announcements-container {
  min-height: 100%;
}

.announcements-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h3 {
  margin: 0;
  color: #333;
}

.announcements-list {
  padding: 20px 0;
}

.announcement-item {
  margin-bottom: 20px;
}

.announcement-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.announcement-header h4 {
  margin: 0;
  color: #333;
  font-size: 16px;
}

.announcement-content {
  margin-top: 10px;
  color: #666;
  line-height: 1.6;
  white-space: pre-wrap;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

:deep(.el-timeline-item__node) {
  background-color: #409EFF;
}

:deep(.el-timeline-item__tail) {
  border-left-color: #e4e7ed;
}

:deep(.el-timeline-item__timestamp) {
  color: #909399;
}
</style> 